﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using gbL.NetBoard.Helpers;

namespace gbL.NetBoard.Tests
{
    [TestFixture]
    public class TypedMessageListenerTests
    {
        private class Greeting
        {
            public string Text { get; set; }
        }

        private class GreetingListener : TypedMessageListener<Greeting>
        {
            public GreetingListener(Blackboard blackboard) : base(blackboard) { }

            public List<Greeting> added = new List<Greeting>();
            public List<Greeting> removed = new List<Greeting>();

            protected override void OnMessageAdded(Greeting message)
            {
                added.Add(message);
            }

            protected override void OnMessageRemoved(Greeting message)
            {
                removed.Add(message);
            }
        }

        [Test]
        public void Helper_should_fire_when_greeting_added()
        {
            // A
            Blackboard domain = new Blackboard();
            GreetingListener listener = new GreetingListener(domain);
            // A
            domain.Add(null, this, new Greeting() { Text = "Bonjour" });
            // A
            Assert.That(listener.added, Has.Count.EqualTo(1));
            Assert.That(listener.added[0].Text, Is.EqualTo("Bonjour"));
        }

        [Test]
        public void Helper_should_fire_when_greeting_removed()
        {
            // A
            Blackboard domain = new Blackboard();
            GreetingListener listener = new GreetingListener(domain);
            domain.Add(null, this, new Greeting() { Text = "Bonjour" });
            // A
            domain.Take(x => x.From.Equals(this));
            // A
            Assert.That(listener.removed, Has.Count.EqualTo(1));
            Assert.That(listener.removed[0].Text, Is.EqualTo("Bonjour"));
        }

        [Test]
        public void Helper_should_not_fire_when_not_greeting()
        {
            // A
            Blackboard domain = new Blackboard();
            GreetingListener listener = new GreetingListener(domain);
            // A
            domain.Add(null, this, "Something other than a Greeting object");
            // A
            Assert.That(listener.added, Is.Empty);
        }
    }
}
